﻿using Vision001.Helper;
using Vision001.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using System.Windows;
using Common;

namespace Vision001.Procedure
{
    public delegate void UpdatestationHandler(int id, string v1, string v2, string v3, bool res);

    /// <summary>
    /// 程序所有的测试方法
    /// </summary>

    public static class allTestMethods
    {
        public static UpdatestationHandler UpdateWorkStation;

        /// <summary>
        /// 读取模拟量电压
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public static bool ReadVority(TestMethodModel config)
        {
            try
            {
                bool Result = true;
                string item1 = Convert.ToString(config.item1);
                double item2 = Convert.ToDouble(config.item2);
                double item3 = Convert.ToDouble(config.item3);
                string item4 = Convert.ToString(config.item4);
                double item5 = Convert.ToDouble(config.item5);
                double item6 = Convert.ToDouble(config.item6);
                string item7 = Convert.ToString(config.item7);
                double item8 = Convert.ToDouble(config.item8);
                double item9 = Convert.ToDouble(config.item9);
                int item10 = Convert.ToInt32(config.item10);
                int item11 = Convert.ToInt32(config.item11);
                int item12 = Convert.ToInt32(config.item12);
                int item13 = Convert.ToInt32(config.item13);
                int item14 = Convert.ToInt32(config.item14);
                int item15 = Convert.ToInt32(config.item15);
                double item16 = Convert.ToDouble(config.item16);//测试项1补偿
                double item17 = Convert.ToDouble(config.item17);//测试项2补偿
                double item18 = Convert.ToDouble(config.item18);//测试项3补偿

                //读取测试项目1数值并判断
                double t6value24 = Global._allZsAnalogOutputsDatas24[item14][item15 - 1] + item16;
                bool Result1 = Rangeitem1Andiem2(item1, item2, item3, t6value24);
                //读取测试项目2数值并判断
                double t2value24 = Global._allZsAnalogOutputsDatas24[item10][item11 - 1] + item17; ;
                double t3value24 = Global._allZsAnalogOutputsDatas24[item12][item13 - 1] + item17;
                bool Result2 = Rangeitem1Andiem2(item4, item5, item6, t2value24);
                bool Result3 = Rangeitem1Andiem2(item4, item5, item6, t3value24);
                //读取测试项目3数值并判断
                double t2value33 = Global._allZsAnalogOutputsDatas33[item10][item11 - 1] + item18;
                double t3value33 = Global._allZsAnalogOutputsDatas33[item12][item13 - 1] + item18;
                bool Result4 = Rangeitem1Andiem2(item7, item8, item9, t2value33);
                bool Result5 = Rangeitem1Andiem2(item7, item8, item9, t3value33);
                if (!Result1 || !Result2 || !Result3 || !Result4 || !Result5)
                {
                    Result = false;
                }
                //更新操作界面
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    UpdateWorkStation.Invoke(config.Group, t6value24.ToString(), t2value24.ToString() + "," + t3value24.ToString(), t2value33.ToString() + "," + t3value33.ToString(), Result);
                }));

                return Result;
            }
            catch (Exception e)
            {
                Mylog.Error(e);
                return false;
            }
        }

        /// <summary>
        /// 设置io,存储电压模拟量
        /// </summary>
        /// <param name="config"></param>
        public static void SetIoOutPut(TestMethodModel config)
        {
            string item1 = Convert.ToString(config.item1);//是否全采集
            try
            {
                if (item1 == "1")
                {
                    //全采集
                    //数据保存到所有采集卡数据字典里
                    //先采集24V的数据
                    for (int i = 0; i < 32; i++)
                    {
                        Global._PqwIoTwoFour.SetDO(i, true);
                    }
                    Thread.Sleep(1000);
                    for (int i = 0; i < Global._ZsAnalogInputs.Count; i++)
                    {
                        List<double> value = Global._ZsAnalogInputs[i].GetValue();
                        Global._allZsAnalogOutputsDatas24.Add(i + 1, value);
                    }
                    //在采集3.3V
                    for (int i = 0; i < 32; i++)
                    {
                        Global._PqwIoThreePointThree.SetDO(i, true);
                    }
                    Thread.Sleep(1000);
                    for (int i = 0; i < Global._ZsAnalogInputs.Count; i++)
                    {
                        List<double> value = Global._ZsAnalogInputs[i].GetValue();
                        Global._allZsAnalogOutputsDatas33.Add(i + 1, value);
                    }

                    for (int i = 0; i < 32; i++)
                    {
                        Global._PqwIoTwoFour.SetDO(i, false);
                        Global._PqwIoThreePointThree.SetDO(i, false);
                    }
                }
                else
                {
                    if (Global.isOntStep)
                    {
                        int item2 = Convert.ToInt32(config.item2);//T2电压采集卡站号

                        int item4 = Convert.ToInt32(config.item4);//T3电压采集卡站号

                        int item6 = Convert.ToInt32(config.item6);//T6电压采集卡站号
                        int item8 = Convert.ToInt32(config.item8);//实际工位
                        Global._PqwIoTwoFour.SetDO(item8 - 1, true);
                        Thread.Sleep(500);
                        List<double> value1 = Global._ZsAnalogInputs[item2 - 1].GetValue();
                        List<double> value2 = Global._ZsAnalogInputs[item4 - 1].GetValue();
                        List<double> value3 = Global._ZsAnalogInputs[item6 - 1].GetValue();
                        Global._allZsAnalogOutputsDatas24.Add(item2, value1);
                        Global._allZsAnalogOutputsDatas24.Add(item4, value2);
                        Global._allZsAnalogOutputsDatas24.Add(item6, value3);
                        Global._PqwIoThreePointThree.SetDO(item8 - 1, true);
                        Thread.Sleep(500);
                        List<double> value11 = Global._ZsAnalogInputs[item2 - 1].GetValue();
                        List<double> value22 = Global._ZsAnalogInputs[item4 - 1].GetValue();
                        List<double> value33 = Global._ZsAnalogInputs[item6 - 1].GetValue();
                        Global._allZsAnalogOutputsDatas33.Add(item2, value11);
                        Global._allZsAnalogOutputsDatas33.Add(item4, value22);
                        Global._allZsAnalogOutputsDatas33.Add(item6, value33);
                        Global._PqwIoTwoFour.SetDO(item8 - 1, false);
                        Global._PqwIoThreePointThree.SetDO(item8 - 1, false);
                    }
                    else
                    {
                        //不处理
                    }
                }
            }
            catch (Exception e)
            {
                Mylog.Error(e);
            }
        }

        /// <summary>
        /// 比较函数,Up是上限，Down是下限，value是要比较的值
        /// </summary>
        /// <param name="ComparisonSign"></param>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        /// <returns></returns>
        public static bool Rangeitem1Andiem2(string ComparisonSign, double Down, double Up, double value)
        {
            switch (ComparisonSign)
            {
                case "<":
                    if (value < Down)
                    {
                        return true;
                    }
                    return false;

                case ">":
                    if (value > Up)
                    {
                        return true;
                    }
                    return false;

                case "<=":
                    if (value <= Down)
                    {
                        return true;
                    }
                    return false;

                case ">=":
                    if (value >= Up)
                    {
                        return true;
                    }
                    return false;

                case "<>":
                    if (value > Down && value < Up)
                    {
                        return true;
                    }
                    return false;

                case "><":
                    if (value < Down || value > Up)
                    {
                        return true;
                    }
                    return false;

                case "<==>":
                    if (value >= Down && value <= Up)
                    {
                        return true;
                    }
                    return false;

                case "=><=":
                    if (value <= Down || value >= Up)
                    {
                        return true;
                    }
                    return false;

                case "":

                    return false;

                case null:
                    return false;
            }
            return true;
        }
    }
}